import{
    getNewsType,getNews
}from "../../apis/getNews"
const app = getApp();
const ipUrl = app.globalData.ipUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    agreed:"false",
    agreeNumber:"212",
    collected:"false",
    commitNumber:"537",
    page_show:false,
    navHeight: '',
    menuButtonInfo: {},
    searchMarginTop: 0, // 搜索框上边距
    searchWidth: 0, // 搜索框宽度
    searchHeight: 0 ,// 搜索框高度
    /*搜索框参数*/
    value: '',
    resultList: [],
    typeArray:[],
    newsList:[],
    ipurl:ipUrl
  },
  clickSearch(){
    wx.navigateTo({
        url: `/pages/searchpg/searchpg`
      });
  },
  clickCell: function(event) {
    const newsurl = event.currentTarget.dataset.url;
    wx.navigateTo({
      url: `/pages/webview/webview?url=${encodeURIComponent(newsurl)}`
    });
  },
  onChangeValue(e) {
    const { value } = e.detail;
    const list = [
      'tdesign-vue',
      'tdesign-react',
      'tdesign-miniprogram',
      'tdesign-angular',
      'tdesign-mobile-vue',
      'tdesign-mobile-react',
    ];
    this.setData({
      resultList: value ? list.filter((v) => v.includes(value)) : [],
    });
  },
  onTabsChange(event) {
    console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
  },
  onTabsClick(event) {
    console.log(`Click tab, tab-panel value is ${event.detail.value}.`);
    const nid=this.data.typeArray[event.detail.value].nameid;
    this.loadnews(nid);
  },
  loadnews(nid){
    getNews(nid).then((res)=>{
        this.setData({
            newsList: res.result.newslist
        })
        console.log(this.data.newsList);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getNewsType().then((res) => {
        // 更新 typeArray
        const newTypeArray = this.data.typeArray.concat(res.result.list[0]);
        // 排序
        newTypeArray.sort((a, b) => a.colid - b.colid);
        // 设置排序后的数组
        this.setData({
            typeArray: newTypeArray
        }, () => {
            console.log(this.data.typeArray[0].nameid)
            this.loadnews(this.data.typeArray[0].nameid);
        });
       
    });
    var systeminfo=wx.getSystemInfoSync()
    //console.log(systeminfo.windowHeight)
    this.setData({
      movehight:systeminfo.windowHeight,
      movehight2:systeminfo.windowHeight-100
    })
 
    this.setData({
      menuButtonInfo: wx.getMenuButtonBoundingClientRect()
    })
    console.log(this.data.menuButtonInfo)
    const { top, width, height, right } = this.data.menuButtonInfo
    wx.getSystemInfo({
      success: (res) => {
        const { statusBarHeight } = res
        const margin = top - statusBarHeight
        this.setData({
          navHeight: (height + statusBarHeight + (margin * 2)),
          searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
          searchHeight: height,  // 与胶囊按钮同高
          searchWidth: right - width -20// 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  changeAgreed(event) {
    this.setData({
      agreed:!this.data.agreed
    })

  },
  changeAgreed2(event) {
this.setData({
      agreed:!this.data.agreed
    })
  },
  changeCollected(event) {
    console.log(event.target.id)
    this.data.video[event.target.id].collected = !this.data.video[event.target.id].collected;
    this.setData({
      video: this.data.video
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})